//
//  GYDCameraManagerController.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/19.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import Photos

class GYDCameraManager: NSObject {

    var photoSelectedBlock: ((image:UIImage) -> ())?
    
    func showWithPresentWithViewController(viewController: UIViewController) -> Void {
        
        var rootVC : UIViewController?
        if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized {
            rootVC = GYDUnAllowCameraViewController()
        }else {
            rootVC = GYDCameraViewController()
            (rootVC as! GYDCameraViewController).photoSelectedBlock = { (image) -> () in
                if let tempBlock = self.photoSelectedBlock {
                    tempBlock(image: image)
                }
            }
        }
        let navVC = GYDNavigationController(rootViewController:rootVC!)
        let backBar = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
        rootVC?.navigationItem.backBarButtonItem = backBar
        viewController.presentViewController(navVC, animated: true, completion: nil)
    }
}

class GYDUnAllowCameraViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        self.navigationItem.title = "相册"
        showUnAllowView()
        
        setUpNavigationBar()
    }
    
    func showUnAllowView() -> Void {
        let imageView = UIImageView(image: UIImage(named: "photo_access_guide_camera_os7"))
        let height = (view.width - 120) / 537.0 * 323
        imageView.frame = CGRect(x: 60, y: view.height/2.0 - height + 64, width: view.width - 120, height: height)
        view.addSubview(imageView)
        
        
        let label1 = UILabel(frame: CGRect(x: 0, y: 0, width: view.width, height: 40))
        label1.numberOfLines = 2
        label1.text = "请进入系统 设置 > 隐私 > 照片 \n以允许“微博”访问你的照片"
        label1.textAlignment = NSTextAlignment.Center
        label1.bottom = imageView.top - 40
        label1.textColor = RGB(100, g: 100, b: 100)
        label1.font = UIFont.systemFontOfSize(13)
        view.addSubview(label1)
        
        let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: view.width, height: 20))
        label2.textColor = RGB(40, g: 40, b: 40)
        label2.font = UIFont.systemFontOfSize(13)
        label2.textAlignment = NSTextAlignment.Center
        label2.bottom = label1.top - 10
        label2.text = "高仿微博没有权限访问您的照片"
        view.addSubview(label2)
        
    }
    
    func setUpNavigationBar() -> Void {
        let backBar = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GYDUnAllowCameraViewController.gotoBack))
        self.navigationItem.leftBarButtonItem = backBar
    }
    
    @objc private func gotoBack() -> Void {
        self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
    }
    
}




